package com.labot.webapp.action;

import java.util.List;
import java.util.Locale;

import com.labot.model.MailTemplate;
import com.labot.types.AuditType;
import com.labot.types.MailType;
import com.labot.util.StackTraceUtils;
import com.labot.webapp.action.messages.TextMessagesType;

/**
 * Action for facilitating User Management feature.
 */
public class MailConfigAction extends BaseAction {
    private static final long serialVersionUID = 6765765538712115191L;

    private List<MailTemplate> lista;
    private Long id;
    private MailType mailType;
    private String mail;
    private MailTemplate template;
    
	/**
     * Default: just returns "success"
     *
     * @return "success"
     */
    public String execute() {
        return SUCCESS;
    }
    
    public String cancel() {
    	return CANCEL;
    }

    
    public String sendTestMail() {
    	try {    		
    	    mailEngine.sendMail(mailType, mail, null, null, null, new Locale("ES", "es"));
    	    mailEngine.sendMail(mailType, mail, null, null, null, new Locale("EN", "en"));
    	} catch (Exception e) {
    		String stack = StackTraceUtils.getStackTrace(e);
    		if(stack.length()>100) {
    			stack = stack.substring(0, 1000);
    		}
    		saveErrorMessage(super.getText(TextMessagesType.CONFIGACTION_TEST_MAIL_ERROR.getKey()));
    		saveExceptionMessage( stack + "...");
    	}
    	return SUCCESS;
    }
    
    public String sendPreviewMail() {
    	try {    		
    		String to = super.getUserLogedin().getEmail();
    	    mailEngine.sendMail(template.getSubject_en(), template.getBody_en(), to);
    	    mailEngine.sendMail(template.getSubject_es(), template.getBody_es(), to);
    	} catch (Exception e) {
    		String stack = StackTraceUtils.getStackTrace(e);
    		if(stack.length()>100) {
    			stack = stack.substring(0, 1000);
    		}
    		saveErrorMessage(super.getText(TextMessagesType.CONFIGACTION_TEST_MAIL_ERROR.getKey()));
    		saveExceptionMessage( stack + "...");
    		return INPUT;
    	}
    	super.addActionMessage(super.getText(TextMessagesType.SEND_PREVIEW.getKey()));
    	super.log(AuditType.INFO, "Preview mail: " + template.getName() + " Successfully sent");
    	
    	return INPUT;
    }


    public String list() {
        try {
        	lista = mailTemplateDao.getAll();
        } catch (Exception se) {
        	super.addActionException(StackTraceUtils.getStackTrace(se));
        }
        return SUCCESS;
    }
    
    public String edit() {
    	try {
        	template = mailTemplateDao.get(id);
        } catch (Exception se) {
        	super.addActionException(StackTraceUtils.getStackTrace(se));
        }
        return SUCCESS;
    }

    public String save() {
    	try {    		
    		mailTemplateDao.saveMailTemplate(template);
    	} catch (Exception se) {
            super.addActionException(StackTraceUtils.getStackTrace(se));
        }
    	super.addActionMessage(super.getText(TextMessagesType.EDIT_MAIL_SUCCESS.getKey()));
    	super.log(AuditType.INFO, "Modifications to template: " + template.getName() + " Successfully saved");
    	return SUCCESS;
    }
    
    //-----------------------------------------------------------------------------------------------//
    
	public List<MailTemplate> getLista() {
		return lista;
	}

	public void setLista(List<MailTemplate> lista) {
		this.lista = lista;
	}

	public MailTemplate getTemplate() {
		return template;
	}

	public void setTemplate(MailTemplate template) {
		this.template = template;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public MailType getMailType() {
		return mailType;
	}

	public void setMailType(MailType mailType) {
		this.mailType = mailType;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}
    
    

}
